/**
 * @description 路由守卫
 * @author cleverstone
 */
import auth from '@/auth'

export default function () {
    let $cleverApp = this
    // 获取当前路由
    let currentView = window.location.pathname.replace(/\.html$/, '')
        .replace(/^\/*/, '/')
        .replace(new RegExp('^' + $cleverApp.$config.rootUri), '')
    $cleverApp.$route = {
        view: currentView ? currentView : $cleverApp.$config.indexView,  // 当前视图
        module: '',
        title: '',
        auth: false
    }

    // 注册当前路由
    $cleverApp.$router.forEach(value => {
        if (value.view === $cleverApp.$route.view) {
            $cleverApp.$route.module = value.module
            $cleverApp.$route.title = value.title
            $cleverApp.$route.auth = value.auth
        }
    })

    // 权限校验
    let ifAuth
    if ($cleverApp.$util.isType($cleverApp.$route.auth, 'function')) {
        ifAuth = $cleverApp.$route.auth($cleverApp)
    } else {
        ifAuth = $cleverApp.$route.auth
    }

    if (ifAuth && !auth($cleverApp)) {
        // Redirect default view.
        $cleverApp.$push($cleverApp.$config.redirectView)
    }

    return true
}